//
//  CertificationCell.m
//  Peso Guide
//
//  Created by macbook on 2025/6/15.
//

#import "CertificationCell.h"
#import "IdentityAlertView.h"
#import "AddressAlertView.h"
#import "IQKeyboardManager/IQTextView.h"

@interface CertificationCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titLabel;
@property (nonatomic, strong) UITextField *contentTF;
@property (nonatomic, strong) UIButton *selectButton;

@property (nonatomic, strong) NSString *oneString;
@property (nonatomic, strong) NSString *twoString;
@property (nonatomic, strong) NSString *threeString;

@property (nonatomic, strong) UIImageView *rightImg;

@end

@implementation CertificationCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _titLabel = [PesoKit createLabelWithFrame:CGRectMake(30, 0, 150, 61) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 15)];
        _titLabel.numberOfLines = 0;
        [self.contentView addSubview:_titLabel];
        
        _contentTF = [[UITextField alloc] initWithFrame:CGRectMake(180, 0, KScreenWidth-30-180, 61)];
        _contentTF.textColor = ColorHex(@"#0E1D31");
        _contentTF.backgroundColor = [UIColor clearColor];
        _contentTF.font = FONT(FontWeightStyleMedium, 15);
        _contentTF.textAlignment = NSTextAlignmentRight;
        _contentTF.delegate = self;
        [self.contentView addSubview:_contentTF];
        
        _selectButton = [PesoKit createButtonWithFrame:CGRectMake(180, 0, KScreenWidth-30-180, 61) backGroundColor:[UIColor clearColor] title:@"" titleColor:ColorHex(@"#999999") font:FONT(FontWeightStyleMedium, 15)];
        _selectButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _selectButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_selectButton addTarget:self action:@selector(selectButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_selectButton];
        
        UIView *lineView = [PesoKit createViewWithFrame:CGRectMake(30, 61-0.5, KScreenWidth-30*2, 0.5) backGroundColor:ColorHex(@"#EEEEEE")];
        [self.contentView addSubview:lineView];
        
        _rightImg = [PesoKit createImageViewWithFrame:CGRectMake(KScreenWidth-18-7, (61-12)/2.0, 7, 12) imageNamed:@"ic_product_right_icon"];
        _rightImg.hidden = YES;
        [self.contentView addSubview:_rightImg];
    }
    return self;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.identityUserJobInfoVC) {
        NSMutableDictionary *dataDic = [self.identityUserJobInfoVC.dataArray safeObjectAtIndex:_index];
        [dataDic setObject:textField.text forKey:@"fulfill"];
    }
    
    if (self.identityUserInfoVC) {
        NSMutableDictionary *dataDic = [self.identityUserInfoVC.dataArray safeObjectAtIndex:_index];
        [dataDic setObject:textField.text forKey:@"fulfill"];
    }
}

- (void)selectButtonClick {
    //anlikea-enum  anlikeb-txt   anlikec-citySelect
    NSString *resolving = [NSString stringWithFormat:@"%@",[_dataDic safeObjectForKey:@"resolving"]];
    NSMutableArray *chain = [NSMutableArray safeArrayWithArray:[_dataDic safeObjectForKey:@"chain"]];
    
    if ([resolving isEqualToString:@"anlikea"]) {
        IdentityAlertView *alertView = [[IdentityAlertView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        alertView.dataArray = chain;
        // 设置回调闭包
        __weak typeof(self) weakSelf = self;
        alertView.selectionHandler = ^(NSDictionary *selectedDict) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithCapacity:0];
            if (strongSelf.identityUserJobInfoVC) {
                dataDic = [strongSelf.identityUserJobInfoVC.dataArray safeObjectAtIndex:strongSelf.index];
            }
            if (strongSelf.identityUserInfoVC) {
                dataDic = [strongSelf.identityUserInfoVC.dataArray safeObjectAtIndex:strongSelf.index];
            }
            NSString *unboundlocalerror = [NSString stringWithFormat:@"%@",[selectedDict safeObjectForKey:@"unboundlocalerror"]];
            NSString *restriction = [NSString stringWithFormat:@"%@",[selectedDict safeObjectForKey:@"restriction"]];

            [dataDic setObject:unboundlocalerror forKey:@"fulfill"];
            
            strongSelf.contentTF.text = restriction;
            [strongSelf.selectButton setTitle:restriction forState:0];
            [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
        };
        [[AppTools getCurrentVC].view addSubview:alertView];
    }
    
    if ([resolving isEqualToString:@"anlikec"]) {
        AddressAlertView *alertView = [[AddressAlertView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        __weak typeof(self) weakSelf = self;
        alertView.selectionHandler = ^(NSDictionary *selectedDict) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithCapacity:0];
            if (strongSelf.identityUserJobInfoVC) {
                dataDic = [strongSelf.identityUserJobInfoVC.dataArray safeObjectAtIndex:strongSelf.index];
            }
            if (strongSelf.identityUserInfoVC) {
                dataDic = [strongSelf.identityUserInfoVC.dataArray safeObjectAtIndex:strongSelf.index];
            }
            NSDictionary *district = [selectedDict safeObjectForKey:@"district"];
            NSDictionary *city = [selectedDict safeObjectForKey:@"city"];
            NSDictionary *province = [selectedDict safeObjectForKey:@"province"];

            NSString *districtRestriction = [NSString stringWithFormat:@"%@",[district safeObjectForKey:@"restriction"]];
//            NSString *districtCauses = [NSString stringWithFormat:@"%@",[district safeObjectForKey:@"causes"]];
            
            NSString *cityRestriction = [NSString stringWithFormat:@"%@",[city safeObjectForKey:@"restriction"]];
//            NSString *cityCauses = [NSString stringWithFormat:@"%@",[city safeObjectForKey:@"causes"]];
            
            NSString *provinceRestriction = [NSString stringWithFormat:@"%@",[province safeObjectForKey:@"restriction"]];
//            NSString *provinceCauses = [NSString stringWithFormat:@"%@",[province safeObjectForKey:@"causes"]];

            [dataDic setObject:[NSString stringWithFormat:@"%@|%@|%@",provinceRestriction,cityRestriction,districtRestriction] forKey:@"fulfill"];
            
            strongSelf.contentTF.text = [NSString stringWithFormat:@"%@|%@|%@",provinceRestriction,cityRestriction,districtRestriction];
            [strongSelf.selectButton setTitle:[NSString stringWithFormat:@"%@|%@|%@",provinceRestriction,cityRestriction,districtRestriction] forState:0];
            [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
        };
        [[AppTools getCurrentVC].view addSubview:alertView];
    }
}

- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    NSString *accidental = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"accidental"]];
    NSString *structures = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"structures"]];
    //anlikea-enum  anlikeb-txt   anlikec-citySelect
    NSString *resolving = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"resolving"]];
    NSString *transforms = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"transforms"]];
    
    NSString *fulfill = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"fulfill"]];
    NSMutableArray *chain = [NSMutableArray safeArrayWithArray:[_dataDic safeObjectForKey:@"chain"]];

    self.titLabel.text = accidental;
    self.contentTF.placeholder = structures;
    [self.selectButton setTitle:structures forState:0];
    
    if (transforms.integerValue == 1) {
        self.contentTF.keyboardType = UIKeyboardTypeNumberPad;
    }else {
        self.contentTF.keyboardType = UIKeyboardTypeDefault;
    }
    
    if ([resolving isEqualToString:@"anlikea"] || [resolving isEqualToString:@"anlikec"]) {
        self.contentTF.hidden = YES;
        self.selectButton.hidden = NO;
    }
    
    if ([resolving isEqualToString:@"anlikeb"]) {
        self.contentTF.hidden = NO;
        self.selectButton.hidden = YES;
    }
    
    if ([resolving isEqualToString:@"anlikec"]) {
        self.rightImg.hidden = NO;
        NSString *addressJson = [[NSUserDefaults standardUserDefaults] objectForKey:@"app_city_json"];
        NSMutableArray *addressArr = [NSMutableArray safeArrayWithArray:[AppTools arrayWithJsonString:addressJson]];
        
        for (int i = 0; i < addressArr.count; i++) {
            NSDictionary *provinceDic = [addressArr safeObjectAtIndex:i];
            NSArray *provinceArr = [provinceDic safeObjectForKey:@"below"];
            NSString *restrictionOne = [NSString stringWithFormat:@"%@",[provinceDic safeObjectForKey:@"restriction"]];
//            NSString *causesOne = [NSString stringWithFormat:@"%@",[provinceDic safeObjectForKey:@"causes"]];
            
            for (int j = 0; j < provinceArr.count; j++) {
                NSDictionary *cityDic = [provinceArr safeObjectAtIndex:j];
                NSArray *cityArr = [cityDic safeObjectForKey:@"below"];
                NSString *restrictionTwo = [NSString stringWithFormat:@"%@",[cityDic safeObjectForKey:@"restriction"]];
//                NSString *causesTwo = [NSString stringWithFormat:@"%@",[cityDic safeObjectForKey:@"causes"]];
                
                for (int l = 0; l < cityArr.count; l++) {
                    NSDictionary *districtDic = [cityArr safeObjectAtIndex:l];
                    NSString *restrictionThree = [NSString stringWithFormat:@"%@",[districtDic safeObjectForKey:@"restriction"]];
//                    NSString *causesThree = [NSString stringWithFormat:@"%@",[districtDic safeObjectForKey:@"causes"]];
                    
                    if (fulfill.length > 0) {
                        NSArray *addressArray = [fulfill componentsSeparatedByString:@"|"];
                        if (addressArray.count > 0) {

                            NSString *one = [addressArray safeObjectAtIndex:0];
                            NSString *two = [addressArray safeObjectAtIndex:1];
                            NSString *three = [addressArray safeObjectAtIndex:2];

                            if ([restrictionOne isEqualToString:one]) {
                                self.oneString = restrictionOne;
                            }
                            
                            if ([restrictionTwo isEqualToString:two]) {
                                self.twoString = restrictionTwo;
                            }
                            
                            if ([restrictionThree isEqualToString:three]) {
                                self.threeString = restrictionThree;

                            }
                            
                            self.contentTF.text = [NSString stringWithFormat:@"%@|%@|%@",self.oneString,self.twoString,self.threeString];
                            [self.selectButton setTitle:[NSString stringWithFormat:@"%@|%@|%@",self.oneString,self.twoString,self.threeString] forState:0];
                            [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
                            
                        }else {
                            self.contentTF.text = restrictionThree;
                            [self.selectButton setTitle:restrictionThree forState:0];
                            [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
                        }
                    }
                    
                }
            }
        }
        
    }else {
        if (chain.count > 0) {
            self.rightImg.hidden = NO;
            for (int i = 0; i < chain.count; i++) {
                NSDictionary *dic = [chain safeObjectAtIndex:i];
                NSString *unboundlocalerror = [NSString stringWithFormat:@"%@",[dic safeObjectForKey:@"unboundlocalerror"]];
                NSString *restriction = [NSString stringWithFormat:@"%@",[dic safeObjectForKey:@"restriction"]];

                if ([unboundlocalerror isEqualToString:fulfill]) {
                    self.contentTF.text = restriction;
                    [self.selectButton setTitle:restriction forState:0];
                    [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
                }
            }
        }else {
            self.rightImg.hidden = YES;
            self.contentTF.text = fulfill;
            [self.selectButton setTitle:fulfill forState:0];
            [self.selectButton setTitleColor:ColorHex(@"#0E1D31") forState:0];
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
